from  openpyxl import  Workbook
import time
import datetime
import os




class data_template:
    Entity = 1
    Operation = 2
    CampaignId = 3
    AdGroupId = 4
    PortfolioId = 5
    AdId = 6
    KeywordId = 7
    ProductTargetingId = 8
    CampaignName = 9
    AdGroupName = 10
    StartDate = 11
    TargetingType = 13
    State = 14
    DailyBudget = 15
    SKU = 16
    ASIN = 17
    AdGroupDefaultBid = 18
    Bid = 19
    KeywordText = 20
    MatchType = 21
    BiddingStrategy = 22
    Placement = 23
    Percentage = 24
    ProductTargetingExpression = 25

class sheet_father:
    def __init__(self, sid, browserName, seller_id, browserOauth, country):
        self.sheet_name = "Sponsored Products Campaigns"
        self.title = ['Product', 'Entity', 'Operation', 'Campaign Id', 'Ad Group Id', 
           'Portfolio Id', 'Ad Id', 'Keyword Id', 'Product Targeting Id', 'Campaign Name', 
           'Ad Group Name', 'Start Date', 'End Date', 'Targeting Type', 'State', 'Daily Budget', 
           'SKU', 'ASIN', 'Ad Group Default Bid', 'Bid', 'Keyword Text', 'Match Type', 'Bidding Strategy', 
           'Placement', 'Percentage', 'Product Targeting Expression']
        # self.file_path = r"C:/Users/Administrator/Desktop/Super Browser/" + browserName + "/ad" + datetime.datetime.now().strftime("%Y-%m-%d") +"/"
        self.file_path = r"G:/Code/ac_lingxing_api/ad_ana/create_sheets/"
        self.file_name = f"bulk_{sid}_"  + str(int(time.time())) + ".xlsx"
        self.save_path = os.path.join(self.file_path, self.file_name)
        self.wb = Workbook()
        self.ws =  self.wb.create_sheet(self.sheet_name, 0)
        self.ws.append(self.title)
        self.wb.remove(self.wb["Sheet"])
        self.info = {
            "sid": sid, 
            "account_name": browserName,
            "seller_id": seller_id,
            "save_path":self.save_path,
            "browserOauth":browserOauth,
            "country":country
        }
    # 创建 广告活动 行
    def create_campaign_lst(self, operation, campaign_id, portfolio_id, campaign_name, targeting_type, budget, bidding_strategy, state, start_data):
        lst_campaign = ['Sponsored Products'] + [''] * 25
        lst_campaign[data_template.Operation] = operation
        lst_campaign[data_template.Entity] = 'campaign'
        lst_campaign[data_template.CampaignId] = campaign_id
        lst_campaign[data_template.PortfolioId] = portfolio_id
        lst_campaign[data_template.CampaignName] = campaign_name
        lst_campaign[data_template.TargetingType] = targeting_type
        lst_campaign[data_template.State] = state
        lst_campaign[data_template.DailyBudget] = budget
        lst_campaign[data_template.BiddingStrategy] = bidding_strategy
        lst_campaign[data_template.StartDate] = start_data
        
        
        return lst_campaign
    
    # 创建竞价调整行
    def create_bid_adjust_lst(self, operation, campaign_id, placement, percentage):
        lst_bid_adjust = ['Sponsored Products'] + [''] * 25
        lst_bid_adjust[data_template.Operation] = operation
        lst_bid_adjust[data_template.Entity] = 'Bidding Adjustment'
        lst_bid_adjust[data_template.CampaignId] = campaign_id
        lst_bid_adjust[data_template.Placement] = placement
        lst_bid_adjust[data_template.Percentage] = percentage
        return lst_bid_adjust
    
    # 创建 广告组行
    def create_ad_group_lst(self, operation, campaign_id, ad_group_id, ad_group_name, state, bid):
        lst_ad_group = ['Sponsored Products'] + [''] * 25
        lst_ad_group[data_template.Operation] = operation
        lst_ad_group[data_template.Entity] = 'ad group'
        lst_ad_group[data_template.CampaignId] = campaign_id
        lst_ad_group[data_template.AdGroupId] = ad_group_id
        lst_ad_group[data_template.AdGroupName] = ad_group_name
        lst_ad_group[data_template.State] = state
        lst_ad_group[data_template.AdGroupDefaultBid] = bid
        return lst_ad_group
    
    # 创建商品广告行
    def create_product_ad_lst(self, operation, campaign_id, ad_group_id,state, sku):
        lst_product_ad = ['Sponsored Products'] + [''] * 25
        lst_product_ad[data_template.Operation] = operation
        lst_product_ad[data_template.Entity] = 'product ad'
        lst_product_ad[data_template.CampaignId] = campaign_id
        lst_product_ad[data_template.AdGroupId] = ad_group_id
        lst_product_ad[data_template.State] = state
        lst_product_ad[data_template.SKU] = sku
        return lst_product_ad
    
    # 创建关键词行
    def create_kw_lst(self, operation, campaign_id, ad_group_id,state, bid, Keyword_text, match_type):
        lst_kw = ['Sponsored Products'] + [''] * 25
        lst_kw[data_template.Operation] = operation
        lst_kw[data_template.Entity] = 'Keyword'
        lst_kw[data_template.CampaignId] = campaign_id
        lst_kw[data_template.AdGroupId] = ad_group_id
        lst_kw[data_template.State] = state
        lst_kw[data_template.Bid] = bid
        lst_kw[data_template.KeywordText] = Keyword_text
        lst_kw[data_template.MatchType] = match_type
        return lst_kw
    
    def create_prod_target_lst(self, operation, campaign_id, ad_group_id,state, bid, product_targeting_expression):
        lst_kw = ['Sponsored Products'] + [''] * 25
        lst_kw[data_template.Operation] = operation
        lst_kw[data_template.Entity] = 'Product targeting'
        lst_kw[data_template.CampaignId] = campaign_id
        lst_kw[data_template.AdGroupId] = ad_group_id
        lst_kw[data_template.State] = state
        lst_kw[data_template.Bid] = bid
        lst_kw[data_template.ProductTargetingExpression] = product_targeting_expression
        return lst_kw
    
    
    def add_data(self, data_lst):
        self.ws.append(data_lst)

    def save_file(self):
        if self.ws.max_row > 1:
            if not os.path.exists(self.file_path):
                os.makedirs(self.file_path)
            
            self.wb.save(self.save_path)